<?php
class TaskAction extends BaseAction
{
    public function index()
    {

    }

    /**
     * 活动延期推送
     */
    private function pushActivityExtension()
    {
        $TaskConModel = $this->MTASK();
        //延期活动处理
        unset($map);
        $count = 0;
        $fields = 'tid,mid,fid,bid';
        $map['status'] = 70; //延期
        $tasks = $TaskConModel->table('m_queue')->field($fields)->where($map)->select();
        if ($tasks) {
            $BrandNewsModel = M('BrandNews');
            $data = array();
            foreach ($tasks as $task) {
                unset($map);
                //获取延期信息
                $map['status'] = 1;
                $map['tid'] = $task['tid'];
                $ae = $TaskConModel->table('m_activity_extension')->where($map)->find();
                Log::write(json_encode($ae));
                //找到对应活动信息
                unset($map);
                $map['mid'] = $task['mid'];
                $map['fid'] = $task['fid'];
                $map['bid'] = $task['bid'];
                $new = $BrandNewsModel->field('id')->where($map)->order('id DESC')->find();
                Log::write(json_encode($new));
                //修改活动结束时间
                if ($new && $ae) {
                    unset($data);
                    $data['id'] = $new['id'];
                    $data['end_time'] = $ae['end_time'];
                    $data['last_update'] = time();
                    $rs = $BrandNewsModel->save($data);
                    if ($rs) {
                        //修改推迟任务状态
                        unset($data);
                        $data['status'] = 100;
                        $TaskConModel->table('m_activity_extension')->where('tid=' . $task['tid'])->save($data);
                        //修改队列信息
                        unset($data);
                        $data['status'] = 100;
                        $TaskConModel->table('m_queue')->where('tid=' . $task['tid'])->save($data);
                        //计数
                        $count = $count + 1;

                        //添加活动时间统计
                    }
                }
            }
            unset($map);
            $map['status'] = 100;
            $TaskConModel->table('m_queue')->where($map)->delete();
            Log::write('延期  - ' . count($tasks) . ' - 成功处理 ' . $count);
        }
    }

    /**
     * 推送新的活动信息
     */
    private function pushNews()
    {
        $_run_start_time = microtime(TRUE);
        //获取等待 6审核通过 任务
        $TaskConModel = $this->MTASK();
        $map = array();
        $count = 0;
        $fields = 'tid';
        $map['status'] = 9;
        $tasks = $TaskConModel->table('m_queue')->field($fields)->where($map)->select();
        log::write(json_encode($tasks));
        if ($tasks) {
            unset($map);
            unset($fields);
            $data = array();
            //获取字段
            $fields = 'id,uid,title,start_time,end_time,mid,fid,bid,content,keyword';
            // mall 库
            $FloorBrandModel = M('FloorBrand');
            $FileManagedNewModel = M('FileManagedNew');
            $BrandNewsModel = M('BrandNews');
            // task 库的
            foreach ($tasks as $task) {
                //获取任务信息
                $task = $TaskConModel->table('m_task')->field($fields)->find($task['tid']);
                if ($task) {
                    Log::write(json_encode($task));
                    //获取任务分配信息
                    unset($map);
                    $map['tid'] = $task['id'];
                    $allot = $TaskConModel->table('m_task_allot')->field('id,image')->where($map)->find();
                    Log::write('allot - ' . json_encode($allot));
                    //推送文件 文件成功数据库 插入 文件失败重复进行
                    unset($map);
                    $map['id'] = $allot['image'];
                    $image = $TaskConModel->table('m_file_managed_task')
                        ->field('uid,type,name,uri,mime,size,create_time')
                        ->where($map)
                        ->find();
                    Log::write('image - ' . json_encode($image));
                    //判断对应店铺信息是否存在
                    unset($map);
                    $map['mid'] = $task['mid'];
                    $map['fid'] = $task['fid'];
                    $map['bid'] = $task['bid'];
                    $floorBrand = $FloorBrandModel->where($map)->field('model')->find();
                    $model = $floorBrand['model'];
                    Log::write('Model - ' . $model);
                    //推送文件
                    $time = strftime('%Y/%m/%d', $image['create_time']);
                    $imagePath = UPLOAD_PATH . 'task/' . $time . '/' . $image['uri'];
                    $timeMall = strftime('%Y/%m%d', $image['create_time']);
                    $imageMallDir = UPLOAD_PATH . 'new/' . $timeMall . '/';
                    mk_dir($imageMallDir); //循环创建目录
                    $imageMallPath = $imageMallDir . $image['uri'];
                    $isCopy = copy($imagePath, $imageMallPath);
                    Log::write($imagePath);
                    Log::write($imageMallPath);
                    //数据库信息添加
                    if ($isCopy) {
                        //添加图片信息返回 ID
                        $image['time'] = $image['create_time'];
                        unset($image['create_time']);
                        $fid = $FileManagedNewModel->add($image); //添加图片
                        if ($fid) {
                            //添加优惠信息
                            unset($time);
                            unset($data);
                            $time = time();
                            $data['model'] = $model;
                            $data['title'] = $task['title'];
                            $data['start_time'] = $task['start_time'];
                            $data['end_time'] = $task['end_time'];
                            $data['mid'] = $task['mid'];
                            $data['fid'] = $task['fid'];
                            $data['bid'] = $task['bid'];
                            $data['image'] = $fid;
                            $data['detail'] = $task['content'];
                            $data['create_time'] = $time;
                            $data['last_update'] = $time;
                            $data['editor'] = $task['uid'];
                            $data['keyword'] = $task['keyword'];
                            if (!$BrandNewsModel->add($data)) {
                                Log::write($BrandNewsModel->getDbError());
                            } else {
                                //修改任务状态
                                unset($data);
                                $data['id'] = $task['id'];
                                $data['status'] = 100;
                                $TaskConModel->table('m_task')->save($data);

                                //修改任务分配状态
                                unset($data);
                                $data['id'] = $allot['id'];
                                $data['status'] = 100;
                                $TaskConModel->table('m_task_allot')->save($data);

                                //修改队列信息
                                unset($data);
                                $data['status'] = 100;
                                $TaskConModel->table('m_queue')->where('tid=' . $task['id'])->save($data);
                                $count = $count + 1;
                            }
                        } else {
                            Log::write($FileManagedNewModel->getDbError());
                        }
                    }
                }
            }
            unset($map);
            $map['status'] = 100;
            $TaskConModel->table('m_queue')->where($map)->delete();
            $_run_end_time = microtime(TRUE);
            Log::write('推送 - ' . count($tasks) . ' - 成功处理 ' . $count . ' 用时 ' . ($_run_end_time - $_run_start_time));
        }

    }

    /**
     * 推送活动任务信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function push()
    {
        $this->pushNews();
        $this->pushActivityExtension();
    }
}